package com.yazi.mxz.server.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonView;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.ShoppingCart;
import com.yazi.mxz.common.service.ShoppingCartService;
import com.yazi.mxz.server.response.ShoppingCartListResponse;
import com.yazi.mxz.server.response.view.StoreGoodsFStoreAShoppingsView;

@Controller("shoppingCartController")
@RequestMapping("/shoppingCart")
public class ShoppingCartController extends BaseController {
	private static Logger log = LoggerFactory
			.getLogger(ShoppingCartController.class);

	@Resource
	ShoppingCartService shoppingCartService;

	@RequestMapping("/list")
	@ResponseBody
	@JsonView(StoreGoodsFStoreAShoppingsView.class)
	public ShoppingCartListResponse list() {
		Member loginMember = getLoginMember();
		Long memberId = loginMember.getMemberId();
		log.debug("客户端 -> 获取购物车列表：memberId={}", memberId);
		ShoppingCartListResponse res = new ShoppingCartListResponse();
		try {
			Set<ShoppingCart> sets = loginMember.getShoppingCarts();
			List<ShoppingCart> list = new ArrayList<ShoppingCart>();
			list.addAll(sets);
			res.setSuccessData(list);
			res.setOk();
		} catch (Exception e) {
			log.error("获取购物车列表异常:", e);
			res.setErrorMessage("操作失败,请联系管理员");
		}

		return res;
	}
}
